﻿hpxAdminApp.controller('pccMainController', function (exportService, $rootScope, $interval, $scope,$filter, $stateParams, $state, API_URL, NgTableParams, billService, constantsService, addressService, EXPORT_URL, pccService) {
    $scope.filter = {
        accept_type_id: '',
        accept_type_ids: [],
        accept_type_ids1: '401',
        bill_flaws: '',
        bill_flaws1: '',
        bill_flaws2: 1,
        for_export: '',
        bill_sum_price: '',
        dead_date: '',
        outUsageIsLoan: 0,
        outUsageIsEntrance: 0,
        showTips: false,
        showTime1: false,
        showTimeEm: false,
        showPlus1: false,
        showPlusEm: false,
        showRateEm: false,
        showRate1: false,
        showChoose: false
    }
    //console.log($stateParams)
    if ($scope.filter.bill_sum_price != '' && $scope.filter.dead_date != '') {
        $scope.filter.bill_flaws = 0;
        $scope.filter.bill_flaws2 = 1;
    }
    //$scope.model={}
    if ($stateParams) {
        //$filter('date')(new Date(), 'yyyy-MM-dd')
        $scope.filter.bill_sum_price = $stateParams.bill_sum_price / 10000;
        //$scope.filter.dead_date = $filter('date')($stateParams.dead_date, 'yyyy-MM-dd');
        $scope.filter.dead_date = Number($stateParams.dead_date)
        //$scope.filter.accept_type_ids.push($stateParams.accept_type_ids)
        $scope.filter.accept_type_ids1 = $stateParams.accept_type_ids
        $scope.filter.accept_type_ids.push($stateParams.accept_type_ids)
        if ($stateParams.bill_flaws == 1500) {
            $scope.filter.bill_flaws2 = 1;
            $scope.filter.bill_flaws = 0;
        } else {
            $scope.filter.bill_flaws2 = 2;
            $scope.filter.bill_flaws = 1;
        }
    }
    //提取数字
    function extractNum(str) {
        str = str.replace(/[\u4E00-\u9FA5]/g, "")
        str = str.replace('(', ''),        str = str.replace(')', '');
        var str_before = str.split("-")[0];
        var str_after = str.split("-")[1];
        return [str_before, str_after];
    }

    // 选择承兑类型
    // 如果选择票面金额和到期日期，承兑机构不能选择不限
    $scope.acceptTypeAllChange = function () {
        $scope.filter.accept_type_id = '';
        $scope.filter.accept_type_ids = [];
    }
    $scope.acceptTypeChange = function (acceptTypeId) {
        if ($scope.filter.accept_type_ids.indexOf(acceptTypeId) == -1) {
            $scope.filter.accept_type_ids.push(acceptTypeId);
        } else {
            var index = $scope.filter.accept_type_ids.indexOf(acceptTypeId);
            if (index > -1) {
                $scope.filter.accept_type_ids.splice(index, 1);
            }
        }

        if ($scope.filter.accept_type_ids.length == 7) {
            $timeout(function () {
                $scope.filter.accept_type_id = '';
                $scope.filter.accept_type_ids = [];
            }, 200);
        } else {
            $scope.filter.accept_type_ids = $scope.filter.accept_type_ids;
        }
    }

    $scope.acceptTypeChange1 = function (acceptTypeId1) {
        $scope.filter.accept_type_ids1 = acceptTypeId1;
    }
    // 瑕疵
    $scope.flawTypeAllChange = function () {
        $scope.filter.bill_flaws = '';
        $scope.filter.bill_flaws1 = '';
    }
    $scope.flawTypeChange = function (flawId) {
        $scope.filter.bill_flaws1 = flawId;
        if (flawId == 1) {
            $scope.filter.bill_flaws = 0;
        } else {
            $scope.filter.bill_flaws = 1;
        }
    }

    $scope.flawTypeChange1 = function (flawId) {
        $scope.filter.bill_flaws2 = flawId;
        if (flawId == 1) {
            $scope.filter.bill_flaws = 0;
        } else {
            $scope.filter.bill_flaws = 1;
        }
    }

    // 行口
    $scope.exportTypeAllChange = function () {
        $scope.filter.for_export = '';
        $scope.filter.outUsageIsLoan = 0;
        $scope.filter.outUsageIsEntrance = 0;
    }
    $scope.exportTypeChange = function (exportId) {
        $scope.filter.for_export = exportId;
        if (exportId == 1) {
            $scope.filter.outUsageIsLoan = 0;
            $scope.filter.outUsageIsEntrance = 1;
        } else {
            $scope.filter.outUsageIsLoan = 1;
            $scope.filter.outUsageIsEntrance = 0;
        }
    }
    //获取所有求购信息
    $scope.tableParams = new NgTableParams({ 'sorting': { 'discountAmount': 'desc' } }, {
        getData: function (params) {
            return pccService.getBillPcc(params, $scope.filter).then(function (result) {
                if (result) {
                    var data = result;
                    return data;
                }
            })
        }
    });
    //查看单张求购票据详情
    $scope.showDetailData = function (item) {
        //弹出窗口
        $('#modal-read').modal('show');
        pccService.getPccBillOfferDetailPC(item.billOfferDetailId, item.outUsage).then(function (data) {
            $scope.pccDetail = data;
        });
    };
    //$scope.tableParams = new NgTableParams({ 'sorting': { 'create_time': 'desc' } }, {
    //    getData: function (params) {
    //        return billService.billHoldingCenterList(params, $scope.filter).then(function (data) {
    //            $scope.first = $scope.getFirst(params);
    //            return data;
    //        });
    //    }
    //});
    //刷新
    $scope.reflash = function () {
        $scope.tableParams.reload();
    }
    //按条件筛选
    $scope.filterWantBill = function () {
        $scope.filter.dead_date1 = $filter('date')($scope.filter.dead_date, 'yyyy-MM-dd');
        $scope.filter.showChoose = true;
        if ($scope.filter.dead_date != '' && $scope.filter.bill_sum_price != '') {
            $scope.filter.accept_type_ids2 = []
            $scope.filter.accept_type_ids2.push($scope.filter.accept_type_ids1);
            if ($scope.filter.bill_flaws2 == 1) {
                $scope.filter.bill_flaws = 0;
            } else {
                $scope.filter.bill_flaws = 1;
            }

            $scope.filter.accept_type_ids = $scope.filter.accept_type_ids2;
            $scope.filter.showPlus1 = true;
            $scope.filter.showRate1 = false;
            $scope.filter.showTime1 = false;
            $scope.filter.showPlusEm = true;


        }
        $scope.tableParams = new NgTableParams({ 'sorting': { 'billEveryPlus': 'asc' } }, {
            getData: function (params) {
                var p1 = params.page();
                return pccService.getBillPcc1(params, $scope.filter, p1).then(function (result) {
                    if (result) {
                        var data = result;
                        return data;
                    } else {
                        $scope.filter.showTips = true;
                    }
                })
            }
        });
    }
    // 快速排序 报价时间
    $scope.billOfferTimeSort = function () {
        $scope.filter.showTime1 = true,
        $scope.filter.showPlus1 = false;
        $scope.filter.showRate1 = false;

        if (!$scope.filter.showTimeEm) {
            $scope.filter.showTimeEm = true;
            $scope.filter.dead_date1 = $filter('date')($scope.filter.dead_date, 'yyyy-MM-dd');
            $scope.tableParams = new NgTableParams({ 'sorting': { 'offerTime': 'asc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return pccService.getBillPcc1(params, $scope.filter, p1).then(function (result) {
                        if (result) {
                            var data = result;
                            return data;
                        } else {
                            $scope.filter.showTips = true;
                        }
                    })
                }
            });
        } else {
            $scope.filter.showTimeEm = false;
            $scope.filter.dead_date1 = $filter('date')($scope.filter.dead_date, 'yyyy-MM-dd');
            $scope.tableParams = new NgTableParams({ 'sorting': { 'offerTime': 'desc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return pccService.getBillPcc1(params, $scope.filter, p1).then(function (result) {
                        if (result) {
                            var data = result;
                            return data;
                        } else {
                            $scope.filter.showTips = true;
                        }
                    })
                }
            });
        }
    }

    // 快速排序 每十万扣
    $scope.billEveryPlusSort = function () {
        $scope.filter.showPlus1 = true;
        $scope.filter.showRate1 = false;
        $scope.filter.showTime1 = false;

        if (!$scope.filter.showPlusEm) {
            $scope.filter.showPlusEm = true;
            $scope.filter.dead_date1 = $filter('date')($scope.filter.dead_date, 'yyyy-MM-dd');
            $scope.tableParams = new NgTableParams({ 'sorting': { 'billEveryPlus': 'asc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return pccService.getBillPcc1(params, $scope.filter, p1).then(function (result) {
                        if (result) {
                            var data = result;
                            return data;
                        } else {
                            $scope.filter.showTips = true;
                        }
                    })
                }
            });
        } else {
            $scope.filter.showPlusEm = false;
            $scope.filter.dead_date1 = $filter('date')($scope.filter.dead_date, 'yyyy-MM-dd');
            $scope.tableParams = new NgTableParams({ 'sorting': { 'billEveryPlus': 'desc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return pccService.getBillPcc1(params, $scope.filter, p1).then(function (result) {
                        if (result) {
                            var data = result;
                            return data;
                        } else {
                            $scope.filter.showTips = true;
                        }
                    })
                }
            });
        }
    }

    // 快速排序 加价利率
    $scope.billRateSort = function () {
        $scope.filter.showPlus1 = false;
        $scope.filter.showRate1 = true;
        $scope.filter.showTime1 = false;

        if (!$scope.filter.showRateEm) {
            $scope.filter.showRateEm = true;

            $scope.filter.dead_date1 = $filter('date')($scope.filter.dead_date, 'yyyy-MM-dd');
            $scope.tableParams = new NgTableParams({ 'sorting': { 'billRate': 'desc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return pccService.getBillPcc1(params, $scope.filter, p1).then(function (result) {
                        if (result) {
                            var data = result;
                            return data;
                        } else {
                            $scope.filter.showTips = true;
                        }
                    })
                }
            });
        } else {
            $scope.filter.showRateEm = false;

            $scope.filter.dead_date1 = $filter('date')($scope.filter.dead_date, 'yyyy-MM-dd');
            $scope.tableParams = new NgTableParams({ 'sorting': { 'billRate': 'desc' } }, {
                getData: function (params) {
                    var p1 = params.page();
                    return pccService.getBillPcc1(params, $scope.filter, p1).then(function (result) {
                        if (result) {
                            var data = result;
                            return data;
                        } else {
                            $scope.filter.showTips = true;
                        }
                    })
                }
            });
        }
    }
    // 清空
    $scope.clear = function () {
        //window.location.reload();
        $scope.filter = {
            accept_type_id: '',
            accept_type_ids: [],
            accept_type_ids1: '401',
            bill_flaws: '',
            bill_flaws1: '',
            bill_flaws2: 1,
            for_export: '',
            bill_sum_price: '',
            dead_date: '',
            outUsageIsLoan: 0,
            outUsageIsEntrance: 0,
            showTips: false,
            showTime1: false,
            showTimeEm: false,
            showPlus1: false,
            showPlusEm: false,
            showRateEm: false,
            showRate1: false,
            showChoose: false
        }
        $scope.reflash();
    }
    
    $("#setup_times").datetimepicker({
        format: "yyyy-mm-dd",
        autoclose: true,
        minView: "month",
        maxView: "decade",
        todayBtn: true,
        pickerPosition: "bottom-left",
        language: 'zh-CN',
        startDate: new Date()
    }).on("click", function (ev) {
        $("#setup_times").datetimepicker("setEndDate", $("#setup_times").val());
    }).on('changeDate', function (e) {
        $scope.filter.setupTimes = $("#setup_times").val();
        $scope.filter.dead_date = $scope.filter.setupTimes;
    });
    //点击详细
    //$scope.resd = function (item) {
    //    $scope.models = item;         //获取某条客户资料详细信息
    //    $scope.initBillFlaw();
    //    //弹出详细窗口
    //    $('#modal-read').modal('show');
    //    $('.jqzoom').imagezoom();       //图片放大功能
    //};

});
